const Actions = require('./actions');
const DB = require('app/db');

module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {

    },

    onFetch: function (params, cb) {
        let t = this;
        DB.ParentNotice.get_notice_detail({
            schoolId: params.schoolId,
            noticeId: params.noticeId,
            studentId: params.studentId
        })
            .then((content) => {
                if (content.code == '1') {
                    content = content.data;
                    if (JSON.stringify(content) != '{}') {
                        if (content.fileList && content.fileList instanceof Array && content.fileList.length > 0) {
                            for (let item of content.fileList) {
                                let { url, fileName, type, size } = item;
                                item.imgUrl = "https://www.zxjydev.com" + url;
                                item.title = fileName;
                            }
                        }
                        this.data = {
                            content: content,
                            title: content.noticeTitle,
                            textareaContent: content.noticeMsg,
                            sender: content.releaseName,
                            senderData: content.noticeTime,
                            noticeRang: content.noticeRang,
                            fileList: content.fileList,
                            replayFlag: content.replayFlag,
                            labelText: content.noticeType,
                            replayList: content.replayList,
                            noticeUrl: content.noticeUrl,
                            releasePosition: content.releasePosition
                        }
                        cb && cb(0);
                        this.updateComponent();
                    }
                }
            })
            .catch(function (error) {

            });
    },
    onGetReplayList: function (params, cb) {
        let t = this;
        DB.ParentNotice.get_replay_list({
            schoolId: params.schoolId,
            studentId: params.studentId,
            noticeId: params.noticeId,
            pageIndex: 1,
            userType: params.userType,
            replayId: '668744' || params.replayId//家长id
        })
            .then((content) => {
                let schoolId = content.check.schoolId;
                let studentId = content.check.studentId;
                content = content.data.replayList;
                var list = [];
                var obj = {};
                if (content && content.length > 0) {
                    for (let item of content) {
                        obj["studentId"] = studentId;
                        obj["badge"] = false;
                        obj.imgUrl = item.imgurl || 'https://img.alicdn.com/tps/TB15YftJFXXXXafXpXXXXXXXXXX-699-698.jpg';
                        obj.text = item.content;
                        obj.title = item.name;
                        obj.date = item.replayDate;
                        obj.readStatus = item.readStatus;
                        obj.replayId = item.replayId;
                        obj.readStatus = item.readStatus;
                        obj.replayType = item.replayType;
                        obj.userType = item.userType;
                        list.push(obj);
                        obj = {};
                    }
                }
                t.data.replyList = list;
                t.updateComponent();
            })
            .catch(function (error) {

            });
    },
    onReplayNotice: function ({ data, content } = {}, cb) {
        let params = data;
        DB.ParentNotice.sent_replay_msg({
            schoolId: params.schoolId,
            noticeId: params.noticeId,
            replayId: '668744' || params.replayId,//家长id
            content: content
        })
            .then((content) => {
                if (content.code == 1) {
                    cb && cb(0)
                }
            })
            .catch(function (error) {

            });
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
